<template>
  <div
    class="window"
    :class="{ 
      maximized: window.maximized, 
      minimized: window.minimized,
      active: isActive 
    }"
    :style="windowStyle"
    @mousedown="$emit('activate', window.id)"
  >
    <!-- 窗口标题栏 -->
    <div class="window-titlebar" @mousedown="$emit('dragstart', window, $event)">
      <div class="titlebar-left">
        <div class="window-icon" :style="{ background: window.color }">
          <el-icon :size="16">
            <component :is="window.icon" />
          </el-icon>
        </div>
        <span class="window-title">{{ window.title }}</span>
      </div>
      <div class="titlebar-buttons">
        <button class="titlebar-btn minimize" @click.stop="$emit('minimize', window.id)">
          <el-icon><Minus /></el-icon>
        </button>
        <button class="titlebar-btn maximize" @click.stop="$emit('toggle-maximize', window.id)">
          <el-icon><component :is="window.maximized ? 'CopyDocument' : 'FullScreen'" /></el-icon>
        </button>
        <button class="titlebar-btn close" @click.stop="$emit('close', window.id)">
          <el-icon><Close /></el-icon>
        </button>
      </div>
    </div>

    <!-- 窗口内容 -->
    <div class="window-content">
      <iframe v-if="window.type === 'iframe'" :src="window.url" class="window-iframe"></iframe>
      <component v-else-if="window.component" :is="window.component" />
      <div v-else class="window-placeholder">
        <el-icon :size="64" color="#909399"><component :is="window.icon" /></el-icon>
        <p>{{ window.title }}</p>
        <el-text type="info">窗口内容加载中...</el-text>
      </div>
    </div>

    <!-- 窗口调整大小手柄 -->
    <div 
      v-if="!window.maximized" 
      class="window-resize-handle" 
      @mousedown.stop="$emit('resize-start', window, $event)"
    ></div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { Minus, Close, FullScreen, CopyDocument } from '@element-plus/icons-vue'

const props = defineProps({
  window: {
    type: Object,
    required: true
  },
  isActive: {
    type: Boolean,
    default: false
  }
})

defineEmits(['activate', 'dragstart', 'minimize', 'toggle-maximize', 'close', 'resize-start'])

const windowStyle = computed(() => {
  if (props.window.maximized) {
    return {
      left: 0,
      top: 0,
      width: '100%',
      height: 'calc(100% - 48px)'
    }
  }
  return {
    left: props.window.x + 'px',
    top: props.window.y + 'px',
    width: props.window.width + 'px',
    height: props.window.height + 'px'
  }
})
</script>

<style scoped>
.window {
  position: absolute;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: auto;
}

.window.active {
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.4);
  z-index: 100;
}

.window.minimized {
  display: none;
}

.window.maximized {
  border-radius: 0;
}

/* 窗口标题栏 */
.window-titlebar {
  height: 32px;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  cursor: move;
  flex-shrink: 0;
}

.window.active .window-titlebar {
  background: #fff;
}

.titlebar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.window-icon {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
}

.window-title {
  font-size: 12px;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.titlebar-buttons {
  display: flex;
  gap: 4px;
}

.titlebar-btn {
  width: 32px;
  height: 24px;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
  transition: all 0.2s;
  border-radius: 4px;
}

.titlebar-btn:hover {
  background: rgba(0, 0, 0, 0.05);
}

.titlebar-btn.close:hover {
  background: #e81123;
  color: white;
}

/* 窗口内容 */
.window-content {
  flex: 1;
  overflow: auto;
  background: white;
}

.window-iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.window-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 16px;
  color: #909399;
}

/* 窗口调整大小手柄 */
.window-resize-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 16px;
  height: 16px;
  cursor: nwse-resize;
}
</style>

